<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="代码分析,Android静态代码分析," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Android静态代码分析 [TOC] 最佳项目里面来了很多新的小伙伴，然后每个人的代码风格还不一样，虽然有代码风格文档以及代码review。 但是这些东西需要花费很多人力和时间来做，所以就研究了下静态代码分析，能用工具完成的坚决不用人肉。同时静态代码分析还能解决很多潜在的bug问题。 下面依次对介绍几个Android常用的静态代码分析工具，同时顺便介绍下我厂刘全栈的对静态代码分析做的工具Andr">
<meta name="keywords" content="代码分析,Android静态代码分析">
<meta property="og:type" content="article">
<meta property="og:title" content="Android静态代码分析">
<meta property="og:url" content="http://mycommons.cn/2017/07/11/CodeAnalysis/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Android静态代码分析 [TOC] 最佳项目里面来了很多新的小伙伴，然后每个人的代码风格还不一样，虽然有代码风格文档以及代码review。 但是这些东西需要花费很多人力和时间来做，所以就研究了下静态代码分析，能用工具完成的坚决不用人肉。同时静态代码分析还能解决很多潜在的bug问题。 下面依次对介绍几个Android常用的静态代码分析工具，同时顺便介绍下我厂刘全栈的对静态代码分析做的工具Andr">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://github.com/LiushuiXiaoxia/CodeAnalysis/raw/master/doc/ci.png">
<meta property="og:updated_time" content="2017-10-25T11:45:51.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android静态代码分析">
<meta name="twitter:description" content="Android静态代码分析 [TOC] 最佳项目里面来了很多新的小伙伴，然后每个人的代码风格还不一样，虽然有代码风格文档以及代码review。 但是这些东西需要花费很多人力和时间来做，所以就研究了下静态代码分析，能用工具完成的坚决不用人肉。同时静态代码分析还能解决很多潜在的bug问题。 下面依次对介绍几个Android常用的静态代码分析工具，同时顺便介绍下我厂刘全栈的对静态代码分析做的工具Andr">
<meta name="twitter:image" content="https://github.com/LiushuiXiaoxia/CodeAnalysis/raw/master/doc/ci.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2017/07/11/CodeAnalysis/"/>





  <title>Android静态代码分析 | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2017/07/11/CodeAnalysis/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Android静态代码分析</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-11T19:09:03+08:00">
                2017-07-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/代码分析/" itemprop="url" rel="index">
                    <span itemprop="name">代码分析</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/07/11/CodeAnalysis/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2017/07/11/CodeAnalysis/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Android静态代码分析"><a href="#Android静态代码分析" class="headerlink" title="Android静态代码分析"></a>Android静态代码分析</h1><hr>
<p>[TOC]</p>
<p>最佳项目里面来了很多新的小伙伴，然后每个人的代码风格还不一样，虽然有代码风格文档以及代码review。</p>
<p>但是这些东西需要花费很多人力和时间来做，所以就研究了下静态代码分析，能用工具完成的坚决不用人肉。同时静态代码分析还能解决很多潜在的bug问题。</p>
<p>下面依次对介绍几个Android常用的静态代码分析工具，同时顺便介绍下我厂刘全栈的对静态代码分析做的工具<br><a href="https://github.com/MasonLiuChn/AndroidCodeQuality" target="_blank" rel="external">AndroidCodeQuality</a>。</p>
<p>本文最先发表于<a href="https://github.com/LiushuiXiaoxia/CodeAnalysis" target="_blank" rel="external">Github</a>，如有转载，请注明转载出处。</p>
<h1 id="CheckStyle"><a href="#CheckStyle" class="headerlink" title="CheckStyle"></a>CheckStyle</h1><p><a href="http://checkstyle.sourceforge.net/" target="_blank" rel="external">CheckStyle官网</a></p>
<p>『Checkstyle是一个开发工具用来帮助程序员编写符合代码规范的Java代码。它能自动检查Java代码为空闲的人进行这项无聊(但重要)的任务。』</p>
<p>正如Checkstyle的开发者所言，这个工具能够帮助你在项目中定义和维持一个非常精确和灵活的代码规范形式。当你启动CheckStyle，它会根据所提供的配置文件分析你的Java代码并告诉你发现的所有错误。</p>
<p>Checkstyle会发现大量的问题，特别是在你运用了大量的规则配置，如同你设置了一个非常精确的语法。<br>尽管我通过Gradle使用checkstyle，例如在我进行推送之前，我仍然推荐你为IntellJ/Android Studio使用checkstyle插件(你可以通过Android Studio的工作面板文件/设置/插件直接安装插件)。<br>这种方式下，你可以根据那些为Gradle配置的相同文件在你的工程中使用checkstyle，但是远不止这些，你可以直接在Android Studio中获取带有超链接结果，<br>这些结果通过超链接在你的代码中对应，这是非常有用的(Gradle的这种方式仍然很重要的，因为你可以使用它自动构建系统，如Jenkins)。</p>
<h2 id="集成"><a href="#集成" class="headerlink" title="集成"></a>集成</h2><p>在Android中集成CheckStyle也很简单，只需要在Gradle添加少许配置即可。</p>
<figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div></pre></td><td class="code"><pre><div class="line">apply plugin: <span class="string">'checkstyle'</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// 定义生成文件目录</span></div><div class="line"><span class="keyword">def</span> checkStyleReportPath = <span class="string">"$&#123;project.rootDir&#125;/reports/"</span></div><div class="line"></div><div class="line"><span class="comment">// 在clean时候，自动删除原先生成报告</span></div><div class="line">clean.<span class="keyword">doFirst</span> &#123;</div><div class="line">    <span class="keyword">delete</span> checkStyleReportPath</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">task</span> projectCheckStyle(type: <span class="keyword">Checkstyle</span>) &#123;</div><div class="line">    <span class="keyword">source</span> <span class="string">'src'</span></div><div class="line">    configFile <span class="keyword">file</span>(<span class="string">"checkstyle.xml"</span>) <span class="comment">// 配置样式</span></div><div class="line">    <span class="keyword">include</span> <span class="string">'**/*.java'</span></div><div class="line">    <span class="keyword">exclude</span> <span class="string">'**/gen/**'</span></div><div class="line">    <span class="keyword">classpath</span> = files()</div><div class="line">    ignoreFailures <span class="keyword">true</span> <span class="comment">// 配置是否忽略失败</span></div><div class="line"></div><div class="line">    <span class="comment">// 自定义报告生成路径</span></div><div class="line">    reports &#123;</div><div class="line">        html &#123;</div><div class="line">            destination <span class="string">"$&#123;checkStyleReportPath&#125;/Checkstyle.html"</span></div><div class="line">        &#125;</div><div class="line">        xml &#123;</div><div class="line">            destination <span class="string">"$&#123;checkStyleReportPath&#125;/Checkstyle.xml"</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">tasks.withType(<span class="keyword">Checkstyle</span>).<span class="keyword">each</span> &#123; checkstyleTask -&gt;</div><div class="line">    checkstyleTask.<span class="keyword">doLast</span> &#123;</div><div class="line">        reports.all &#123; report -&gt;</div><div class="line">            <span class="comment">// 检查生成报告中是否有错误</span></div><div class="line">            <span class="keyword">def</span> outputFile = report.destination</div><div class="line">            <span class="keyword">if</span> (outputFile.exists() &amp;&amp; outputFile.text.contains(<span class="string">"&lt;error "</span>) &amp;&amp; !checkstyleTask.ignoreFailures) &#123;</div><div class="line">                <span class="keyword">throw</span> <span class="keyword">new</span> GradleException(<span class="string">"There were checkstyle errors! For more info check $outputFile"</span>)</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// preBuild的时候，执行projectCheckStyle任务</span></div><div class="line">preBuild.dependsOn projectCheckStyle</div></pre></td></tr></table></figure>
<h1 id="PMD"><a href="#PMD" class="headerlink" title="PMD"></a>PMD</h1><p><a href="https://pmd.github.io/" target="_blank" rel="external">PMD官网</a></p>
<p>事实上，PMD是一个工作有点类似Findbugs的强大工具，但是(PMD)直接检查源代码而不是检查字节码(顺便说句，PMD适用很多语言)。<br>(PMD和Findbugs)的核心目标是相同的，通过静态分析方法找出哪些模式引起的bug。因此为什么同时使用Findbugs和PMD呢？<br>好吧！尽管Findbugs和PMD拥有相同的目标，(但是)他们的检查方法是不同的。所以PMD有时检查出的bug但是Findbugs却检查不出来，反之亦然。</p>
<h2 id="集成-1"><a href="#集成-1" class="headerlink" title="集成"></a>集成</h2><figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line">apply plugin: <span class="string">'pmd'</span></div><div class="line"></div><div class="line"><span class="comment">// 定义生成文件目录</span></div><div class="line"><span class="keyword">def</span> pmdReportPath = <span class="string">"$&#123;project.rootDir&#125;/analysis/reports/"</span></div><div class="line"></div><div class="line"><span class="keyword">task</span> projectPmd(type: Pmd) &#123;</div><div class="line">    ignoreFailures = <span class="keyword">true</span></div><div class="line">    ruleSetFiles = files(<span class="string">"pmd.xml"</span>) <span class="comment">// 自定义规则</span></div><div class="line">    ruleSets = []</div><div class="line"></div><div class="line">    <span class="keyword">source</span> <span class="string">'src'</span></div><div class="line">    <span class="keyword">include</span> <span class="string">'**/*.java'</span></div><div class="line">    <span class="keyword">exclude</span> <span class="string">'**/gen/**'</span>, <span class="string">'**/build/**'</span></div><div class="line"></div><div class="line">    <span class="comment">// 定义输出报告</span></div><div class="line">    reports &#123;</div><div class="line">        xml &#123;</div><div class="line">            enabled = <span class="keyword">false</span></div><div class="line">            destination <span class="string">"$pmdReportPath/Pmd.xml"</span></div><div class="line">        &#125;</div><div class="line">        html &#123;</div><div class="line">            enabled = <span class="keyword">true</span></div><div class="line">            destination <span class="string">"$pmdReportPath/Pmd.html"</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">preBuild.dependsOn projectPmd</div></pre></td></tr></table></figure>
<h1 id="FindBugs"><a href="#FindBugs" class="headerlink" title="FindBugs"></a>FindBugs</h1><p><a href="http://findbugs.sourceforge.net/" target="_blank" rel="external">FindBugs官网</a></p>
<p>findbugs是一个分析bytecode并找出其中可疑部分的一个工具。它给项目字节码做一个全面扫描，通过一些通用规则去判断可能潜在的一些问题，比如性能，多线程安全等等。</p>
<p>FindBugs基本上只需要一个程序来做分析的字节码，所以这是非常容易使用。它能检测到常见的错误，如错误的布尔运算符。<br>FindBugs也能够检测到由于误解语言特点的错误，如Java参数调整（这不是真的有可能因为它的参数是传值）。</p>
<h2 id="集成-2"><a href="#集成-2" class="headerlink" title="集成"></a>集成</h2><figure class="highlight gradle"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div></pre></td><td class="code"><pre><div class="line">apply plugin: <span class="string">'findbugs'</span></div><div class="line"></div><div class="line"><span class="comment">// 定义生成文件目录</span></div><div class="line"><span class="keyword">def</span> findbugsReportPath = <span class="string">"$&#123;project.rootDir&#125;/reports/"</span></div><div class="line"></div><div class="line"><span class="keyword">task</span> findbugs(type: FindBugs) &#123;</div><div class="line">    ignoreFailures = <span class="keyword">true</span></div><div class="line">    excludeFilter = <span class="keyword">new</span> <span class="keyword">File</span>(<span class="string">"findbugs.xml"</span>) <span class="comment">// 自定义配置文件</span></div><div class="line">    <span class="keyword">classpath</span> = files()</div><div class="line">    classes = <span class="keyword">fileTree</span>(<span class="string">'build/intermediates/classes/'</span>)</div><div class="line">    effort = <span class="string">'max'</span></div><div class="line"></div><div class="line">    <span class="keyword">source</span> = <span class="keyword">fileTree</span>(<span class="string">'src'</span>)</div><div class="line">    <span class="keyword">include</span> <span class="string">'**/*.java'</span></div><div class="line">    <span class="keyword">exclude</span> <span class="string">'**/gen/**'</span></div><div class="line"></div><div class="line">    <span class="comment">// 定义输出报告</span></div><div class="line">    reports &#123;</div><div class="line">        xml &#123;</div><div class="line">            enabled = <span class="keyword">false</span></div><div class="line">            destination <span class="string">"$findbugsReportPath/FindBugs.xml"</span></div><div class="line">            xml.withMessages <span class="keyword">true</span></div><div class="line">        &#125;</div><div class="line">        html &#123;</div><div class="line">            enabled = <span class="keyword">true</span></div><div class="line">            destination <span class="string">"$findbugsReportPath/FindBugs.html"</span></div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">afterEvaluate &#123;</div><div class="line">    tasks.withType(<span class="keyword">Task</span>).<span class="keyword">each</span> &#123; <span class="keyword">task</span> -&gt;</div><div class="line">        <span class="keyword">task</span>.<span class="keyword">doLast</span> &#123;</div><div class="line">            <span class="keyword">if</span> (<span class="keyword">task</span>.name.startsWith(<span class="string">"assemble"</span>)) &#123;</div><div class="line">                tasks.findByName(<span class="string">"findbugs"</span>).execute()</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="Infer"><a href="#Infer" class="headerlink" title="Infer"></a>Infer</h1><p><a href="http://fbinfer.com/" target="_blank" rel="external">Facebook infer官网</a></p>
<p>Infer 是Facebook的一个开源代码扫描工具。目前在移动端, Infer 是比较好用的一个检查空对象引用的静态扫描工具.</p>
<p>在mac上面使用infer比较简单，参考如下步骤即可，其他平台可以参考<a href="http://fbinfer.com/docs/getting-started.html" target="_blank" rel="external">官网教程</a>。</p>
<p>首先使用brew安装infer</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">brew install infer</div></pre></td></tr></table></figure>
<p>然后在项目根目录运行如下命令即可，最后会在根目录下面生成infer-out目录。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./gradlew clean; infer -- ./gradlew build</div></pre></td></tr></table></figure>
<p>同时Github上面有个Infer的Gradle插件，<a href="https://github.com/uber-common/infer-plugin" target="_blank" rel="external">地址</a>，可以参考下。</p>
<p>使用也比较简单，根目录添加插件依赖</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line">buildscript &#123;</div><div class="line">    repositories &#123;</div><div class="line">        mavenCentral()</div><div class="line">    &#125;</div><div class="line">    dependencies &#123;</div><div class="line">        ...</div><div class="line">        classpath &quot;com.uber:infer-plugin:0.7.4&quot;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Java项目可以使用如下配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">apply plugin: &apos;java&apos;</div><div class="line">apply plugin: &apos;com.uber.infer.java&apos;</div></pre></td></tr></table></figure>
<p>Android的配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">apply plugin: &apos;com.android.application&apos;</div><div class="line">apply plugin: &apos;com.uber.infer.android&apos;</div></pre></td></tr></table></figure>
<p>同时支持配置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">inferPlugin &#123;</div><div class="line">    infer &#123;</div><div class="line">        include = project.files(&quot;&lt;PATH_TO_INCLUDE&gt;&quot;)</div><div class="line">        exclude = project.files(&quot;&lt;PATH_TO_EXCLUDE&gt;&quot;)</div><div class="line">    &#125;</div><div class="line">    eradicate &#123;</div><div class="line">        include = project.files(&quot;&lt;PATH_TO_INCLUDE&gt;&quot;)</div><div class="line">        exclude = project.files(&quot;&lt;PATH_TO_EXCLUDE&gt;&quot;)</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="Lint"><a href="#Lint" class="headerlink" title="Lint"></a>Lint</h1><p><a href="https://developer.android.com/studio/write/lint.html" target="_blank" rel="external">Lint官网</a></p>
<p><a href="http://google.github.io/android-gradle-dsl/2.3/index.html" target="_blank" rel="external">Lint Api</a></p>
<p>Lint是Android官网出的一款功能强大的静态代码分析工具，不仅可以分析代码，还可以分析布局文件，而且还可以硬编码、多余未使用的资源，以及可能出现的bug等。</p>
<p>这里有一篇介绍Lint使用的<a href="http://blog.csdn.net/u010687392/article/details/47835743" target="_blank" rel="external">博客</a></p>
<p>Lint可以在Gradle中集成，当然也可以自定义输出，也可以在Android Studio中单独使用，使用『Analyze』-&gt;『Inspect Code』即可。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">android &#123;</div><div class="line">    lintOptions &#123;</div><div class="line">        abortOnError false</div><div class="line">        disable &apos;LogNotTimber&apos;, &apos;IconMissingDensityFolder&apos;</div><div class="line">        htmlReport true</div><div class="line">        htmlOutput file(&quot;$&#123;project.rootDir&#125;/analysis/reports/lint-report.html&quot;)</div><div class="line">        xmlReport true</div><div class="line">        xmlOutput file(&quot;$&#123;project.rootDir&#125;/analysis/reports/lint-report.xml&quot;)</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="CI集成"><a href="#CI集成" class="headerlink" title="CI集成"></a>CI集成</h1><p><a href="http://www.ruanyifeng.com/blog/2015/09/continuous-integration.html" target="_blank" rel="external">CI介绍</a></p>
<p>互联网软件的开发和发布，已经形成了一套标准流程，最重要的组成部分就是持续集成（Continuous integration，简称CI）。</p>
<p><img src="https://github.com/LiushuiXiaoxia/CodeAnalysis/raw/master/doc/ci.png" alt="CI"></p>
<p>持续集成指的是，频繁地（一天多次）将代码集成到主干，它的好处主要有两个。</p>
<blockquote>
<p>（1）快速发现错误。每完成一点更新，就集成到主干，可以快速发现错误，定位错误也比较容易。</p>
<p>（2）防止分支大幅偏离主干。如果不是经常集成，主干又在不断更新，会导致以后集成的难度变大，甚至难以集成。</p>
</blockquote>
<p>在每次提交代码后，都会有CI服务器给代码做一次编译和代码检查，那么对软件开发的质量也会有很大的提高，如果在智能一点，可以通过邮件、短信、slack通知开发，那么整个开发流程也会更加友好。</p>
<p>后续考虑单独做一个实现一个gradle的插件，自动集成上面几种静态代码检查方式，同时可以实现一件配置，以及报告输出。</p>
<h1 id="相关链接"><a href="#相关链接" class="headerlink" title="相关链接"></a>相关链接</h1><p><a href="http://checkstyle.sourceforge.net/" target="_blank" rel="external">CheckStyle官网</a></p>
<p><a href="http://www.jianshu.com/p/fc2f45a9ee37" target="_blank" rel="external">Android Studio配置CheckStyle</a></p>
<p><a href="https://github.com/MasonLiuChn/AndroidCodeQuality" target="_blank" rel="external">Github AndroidCodeQuality</a></p>
<p><a href="https://pmd.github.io/" target="_blank" rel="external">PMD官网</a></p>
<p><a href="http://findbugs.sourceforge.net/" target="_blank" rel="external">FindBugs官网</a></p>
<p><a href="http://fbinfer.com/" target="_blank" rel="external">Facebook infer官网</a></p>
<p><a href="https://github.com/uber-common/infer-plugin" target="_blank" rel="external">Infer插件</a></p>
<p><a href="http://www.jianshu.com/p/c8a632837bf1" target="_blank" rel="external">Infer参考</a></p>
<p><a href="http://www.jdon.com/48051" target="_blank" rel="external">Infer参考</a></p>
<p><a href="http://blog.csdn.net/u010687392/article/details/47835743" target="_blank" rel="external">Android lint 参考</a></p>
<p><a href="http://google.github.io/android-gradle-dsl/2.3/index.html" target="_blank" rel="external">Lint Api</a></p>
<p><a href="http://www.ruanyifeng.com/blog/2015/09/continuous-integration.html" target="_blank" rel="external">CI介绍</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/代码分析/" rel="tag"># 代码分析</a>
          
            <a href="/tags/Android静态代码分析/" rel="tag"># Android静态代码分析</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/05/21/Android-Modular/" rel="next" title="关于Android模块化我有一些话不知当讲不当讲">
                <i class="fa fa-chevron-left"></i> 关于Android模块化我有一些话不知当讲不当讲
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/07/13/Android-Lombok/" rel="prev" title="Android上使用Lombok">
                Android上使用Lombok <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Android静态代码分析"><span class="nav-number">1.</span> <span class="nav-text">Android静态代码分析</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#CheckStyle"><span class="nav-number">2.</span> <span class="nav-text">CheckStyle</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#集成"><span class="nav-number">2.1.</span> <span class="nav-text">集成</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#PMD"><span class="nav-number">3.</span> <span class="nav-text">PMD</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#集成-1"><span class="nav-number">3.1.</span> <span class="nav-text">集成</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#FindBugs"><span class="nav-number">4.</span> <span class="nav-text">FindBugs</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#集成-2"><span class="nav-number">4.1.</span> <span class="nav-text">集成</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Infer"><span class="nav-number">5.</span> <span class="nav-text">Infer</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Lint"><span class="nav-number">6.</span> <span class="nav-text">Lint</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#CI集成"><span class="nav-number">7.</span> <span class="nav-text">CI集成</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关链接"><span class="nav-number">8.</span> <span class="nav-text">相关链接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2017/07/11/CodeAnalysis/';
          this.page.identifier = '2017/07/11/CodeAnalysis/';
          this.page.title = 'Android静态代码分析';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
